<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品管理</title>
<link rel="stylesheet" type="text/css" href="../static/jquery-easyui-1.3.3/themes/default/easyui.css"></link>
<link rel="stylesheet" type="text/css" href="../static/jquery-easyui-1.3.3/themes/icon.css"></link>
<script type="text/javascript" src="../static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="../static/js/jqsetup.js"></script>
<script type="text/javascript" src="../static/js/jquery.form.js"></script>
<script type="text/javascript" src="../static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">


	var url;
	
	//  商品类别模块管理

	$(document).ready(function() {
		
		$("#tree").tree({
			url:'/admin/goodsType/loadTreeInfo',
			onLoadSuccess:function(node, data){
				$("#tree").tree('expandAll');
			},
			onClick:function(node){
				if(node.attributes.state==0){ // 假如是叶子节点 删除按钮恢复可用
					$('#del').linkbutton('enable');
				}else{
					$('#del').linkbutton('disable');
				}
				$("#dg").datagrid('load',{
					"name":$("#s_name").val(),
					"type.id":node.id
				});
			}
		});
		
	});
	
	
	function openGoodsTypeAddDialog(){
		$("#dlg").dialog("open").dialog("setTitle","新增商品类别");
	}
	
	function deleteGoodsType(){
		var node = $('#tree').tree('getSelected'); // 获取选中节点
		var id=node.id;
		$.post("/admin/goodsType/delete",{id:id},function(result){
			if(result.success){
				$('#tree').tree('reload');
			}else{
				$.messager.alert("系统提示","提交失败，请联系管理员！");
			}
			$('#del').linkbutton('disable');
		},"json");
	}
	
	function saveGoodsType(){
		var goodsTypeName=$("#goodsTypeName").val();
		if(!$("#fm").form("validate")){
			return;
		}
		var node = $('#tree').tree('getSelected'); // 获取选中节点
		var parentId; // 父节点Id
		if(node==null){
			parentId=1;
		}else{
			parentId=node.id;
		}
		$.post("/admin/goodsType/save",{name:goodsTypeName,parentId:parentId},function(result){
			if(result.success){
				$('#tree').tree('reload');
				closeGoodsTypeDialog();
			}else{
				$.messager.alert("系统提示","提交失败，请联系管理员！");
			}
		},"json");
	}
	

	
	function closeGoodsTypeDialog(){
		$("#dlg").dialog("close");
		$("#goodsTypeName").val("");
	}
	
	
	
	// 商品管理模块
	
	
	function formatGoodsTypeId(val,row){
		 return row.type.id;
	 }
	
	function formatGoodsTypeName(val,row){
		return row.type.name;
	}
	
	function searchGoods(){
		$("#dg").datagrid('load',{
			"name":$("#s_name").val() 
		});
	}
	
	function formatPurchasingPrice(val,row){
		return "￥"+val;	
	}
	
	function formatSellingPrice(val,row){
		return "￥"+val;	
	}
	
	
	function openGoodsAddDialog(){
		$("#dlg2").dialog("open").dialog("setTitle","添加商品信息");
		url="admin/goods/save";
		var node = $('#tree').tree('getSelected'); // 获取选中节点
		if(node!=null&&node.id!=1){
			$("#typeId").val(node.id);
			$("#typeName").val(node.text);
		}else{
			$("#typeId").val("");
			$("#typeName").val("");
		}
		$.post("/admin/goods/genGoodsCode",{},function(result){
			$("#code").val(result);
		});
		$("#saveAndAddNextBuuton").show();
	}
	
	function openGoodsModifyDialog(){
		var selectedRows=$("#dg").datagrid("getSelections");
		 if(selectedRows.length!=1){
			 $.messager.alert("系统提示","请选择一条要修改的数据！");
			 return;
		 }
		 var row=selectedRows[0];
		 $("#dlg2").dialog("open").dialog("setTitle","修改商品信息");
		 $("#fm2").form("load",row);
		 $("#typeId").val(row.type.id);
		 $("#typeName").val(row.type.name);
		 url="admin/goods/save?id="+row.id;
		 $("#saveAndAddNextBuuton").hide();
	}
	
	function chooseGoodsType(){
		$("#dlg3").dialog("open").dialog("setTitle","选择商品类别");
		$("#typeTree").tree({
			url:'/admin/goodsType/loadTreeInfo',
			onLoadSuccess:function(node, data){
				var rootNode = $("#typeTree").tree('getRoot'); 
				$("#typeTree").tree('expand',rootNode.target);
			}
		});
	}
	
	
	function saveGoodsTypeChoose(){
		var node = $('#typeTree').tree('getSelected'); // 获取选中节点
		if(node!=null&&node.id!=1){
			$("#typeId").val(node.id);
			$("#typeName").val(node.text);
		}
		$("#dlg3").dialog("close");
	}
	
	function closeGoodsTypeChooseDialog(){
		$("#dlg3").dialog("close");
	}
	
	
	function saveGoods(type){
		var options = {
			url: url, //提交地址：默认是form的action,如果申明,则会覆盖
			beforeSubmit: function(){
				return $("#fm2").form("validate");
			}, //提交前的回调函数
			success: function(result){
				//var result=eval('('+result+')');
				if(result.success){
					$.messager.alert("系统提示","保存成功！");
					resetValue();	
					if(type==2){
						var node = $('#tree').tree('getSelected'); // 获取选中节点
						if(node!=null&&node.id!=1){
							$("#typeId").val(node.id);
							$("#typeName").val(node.text);
						}else{
							$("#typeId").val("");
							$("#typeName").val("");
						}
						$.post("/admin/goods/genGoodsCode",{},function(result){
							$("#code").val(result);
						});						
					}
					if(type==1){
						$("#dlg2").dialog("close");
					}
					$("#dg").datagrid("reload");
				}else{
					$.messager.alert("系统提示",result.errorInfo);
				}
			}, //提交成功后的回调函数
			dataType: "json", //html(默认), xml, script, json...接受服务端返回的类型
			clearForm: true, //成功提交后，是否清除所有表单元素的值
			resetForm: true, //成功提交后，是否重置所有表单元素的值
			timeout: 3000 //限制请求的时间，当请求大于3秒后，跳出请求
		};
		$("#fm2").ajaxSubmit(option);
	}
	
	function resetValue(){
		$("#typeId").val("");
		$("#typeName").val("");
		$("#id").val("");
		$("#name").val("");
		$("#model").val("");
		$("#unit").combobox("setValue","");
		$('#purchasingPrice').numberbox('setValue', "");
		$('#sellingPrice').numberbox('setValue', "");
		$('#minNum').numberbox('setValue', "");
		$("#producer").val("");
		$("#remarks").val("");
	}
	
	function closeGoodsDialog(){
		$("#dlg2").dialog("close");
		resetValue();
	}
	
	function deleteGoods(){
		var selectedRows=$("#dg").datagrid("getSelections");
		if(selectedRows.length==0){
			 $.messager.alert("系统提示","请选择要删除的数据！");
			 return;
		 }
		 var id=selectedRows[0].id;
		 $.messager.confirm("系统提示","您确定要删除这条数据吗？",function(r){
				if(r){
					$.post("/admin/goods/delete",{id:id},function(result){
						if(result.success){
							 $.messager.alert("系统提示","数据已成功删除！");
							 $("#dg").datagrid("reload");
						}else{
							$.messager.alert("系统提示","<font color=red>"+result.errorInfo+"</font>");
						}
					},"json");
				} 
	   });
	}
	
	
	
	
	
	
	// 商品单位模块 
	
	
	function openChooseGoodsUnitDialog(){
		$("#dlg4").dialog("open").dialog("setTitle","单位");
	}
	
	function openGoodsUnitAddDialog(){
		$("#dlg5").dialog("open").dialog("setTitle","添加单位信息");
	}
	
	
	function deleteGoodsUnit(){
		var selectedRows=$("#dg4").datagrid("getSelections");
		if(selectedRows.length!=1){
			 $.messager.alert("系统提示","请选择要删除的数据！");
			 return;
		 }
		 var id=selectedRows[0].id;
		 $.messager.confirm("系统提示","您确定要删除这条数据吗？",function(r){
				if(r){
					$.post("/admin/goodsUnit/delete",{id:id},function(result){
						if(result.success){
							 $("#dg4").datagrid("reload");
						}else{
							$.messager.alert("系统提示",result.errorInfo);
						}
					},"json");
				} 
	   });
	}
	
	function chooseGoodsUnit(){
		var selectedRows=$("#dg4").datagrid("getSelections");
		if(selectedRows.length!=1){
			 $.messager.alert("系统提示","请选择单位！");
			 return;
		}
		var name=selectedRows[0].name;
		$("#unit").combobox("reload");
		$("#unit").combobox("setValue",name);
		$("#dlg4").dialog("close");
	}
	
	function closeGoodsUnitDialog(){
		$("#dlg4").dialog("close");
	}
	
	function saveGoodsUnit(){
		var options = {
			url: "admin/goodsUnit/save",, //提交地址：默认是form的action,如果申明,则会覆盖
			beforeSubmit: function(){
				return $("#fm5").form("validate");
			}, //提交前的回调函数
			success: function(result){
				//var result=eval('('+result+')');
				if(result.success){
					$.messager.alert("系统提示","保存成功！");
					closeGoodsUnitAddDialog();
					$("#dg4").datagrid("reload");
				}else{
					$.messager.alert("系统提示",result.errorInfo);
				}
			}, //提交成功后的回调函数
			dataType: "json", //html(默认), xml, script, json...接受服务端返回的类型
			clearForm: true, //成功提交后，是否清除所有表单元素的值
			resetForm: true, //成功提交后，是否重置所有表单元素的值
			timeout: 3000 //限制请求的时间，当请求大于3秒后，跳出请求
		};
		$("#fm5").ajaxSubmit(option);
	}
	
	function closeGoodsUnitAddDialog(){
		$("#dlg5").dialog("close");
		$("#goodsUnitName").val("");
	}
	
	
	$(function () {  
		 $("[name=token]").val(window.localStorage.getItem('access-token'));
		 $("#dg").datagrid({  
		        //双击事件  
		        onDblClickRow: function (index, row) {  
		        	$("#dlg2").dialog("open").dialog("setTitle","修改商品信息");
		   		   $("#fm2").form("load",row);
		   		   $("#typeId").val(row.type.id);
		   		   $("#typeName").val(row.type.name);
		   		   url="admin/goods/save?id="+row.id;
		   		   $("#saveAndAddNextBuuton").hide();
		        }  
		    });  
		 
		 
		 $("#dg4").datagrid({  
		        //双击事件  
		        onDblClickRow: function (index, row) {  
		        	var selectedRows=$("#dg4").datagrid("getSelections");
		    		var name=selectedRows[0].name;
		    		$("#unit").combobox("reload");
		    		$("#unit").combobox("setValue",name);
		    		$("#dlg4").dialog("close");
		        }  
		    });  
		 
		 
		 
		 
	 });
	

</script>
</head>
<body class="easyui-layout" style="padding-top: 2px">
<div data-options="region:'west',collapsible:false" style="width:200px" title="商品类别" split=true>
	<ul id="tree" class="easyui-tree" style="padding: 10px"></ul>
	<div style="position:absolute;bottom: 10px;left: 15px">
		<a href="javascript:openGoodsTypeAddDialog()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">新增</a>
        <a id="del" href="javascript:deleteGoodsType()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',disabled:true">删除</a>
	</div>
</div>
<div data-options="region:'center'">
		<table id="dg" title="商品管理" class="easyui-datagrid"
		   fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
		   url="/admin/goods/list" fit="true" toolbar="#tb">
		   <thead>
		   	<tr>
		   		<th field="id" width="20" align="center">编号</th>
		   		<th field="code" width="30" align="center">商品编码</th>
		   		<th field="name" width="150" align="center">商品名称</th>
		   		<th field="model" width="50" align="center">型号</th>
		   		<th field="typeId" width="40" align="center" formatter="formatGoodsTypeId" hidden=true>类别ID</th>
		   		<th field="typeName" width="50" align="center" formatter="formatGoodsTypeName">类别</th>
		   		<th field="unit" width="50" align="center" >单位</th>
		   		<th field="purchasingPrice" width="50" align="right" formatter="formatPurchasingPrice">采购价</th>
		   		<th field="sellingPrice" width="50" align="right" formatter="formatSellingPrice">出售价</th>
		   		<th field="minNum" width="50" align="center" >库存下限</th>
		   		<th field="producer" width="200" align="center">生产产商</th>
		   		<th field="remarks" width="150" align="center" hidden=true>备注</th>
		   	</tr>
		   </thead>
		 </table>
		 <div id="tb">
		 	<div>
		 		<a href="javascript:openGoodsAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
		 		<a href="javascript:openGoodsModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
		 		<a href="javascript:deleteGoods()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
		 	</div> 
		 	<div>
		 		&nbsp;商品名称：&nbsp;<input type="text" id="s_name" size="20" onkeydown="if(event.keyCode==13) searchGoods()"/>
		 		<a href="javascript:searchGoods()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
		 	</div>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
		 </div>
</div>

<div id="dlg" class="easyui-dialog" style="width:300px;height:120px;padding: 10px 20px"
   closed="true" buttons="#dlg-buttons"  data-options="onClose:function(){$('#goodsTypeName').val('');}">
   
   <form id="fm" method="post">
	<div>
   		商品类别：&nbsp;<input type="text" id="goodsTypeName" name="name" class="easyui-validatebox" required="true"/>
	</div>
   	
   </form>
 </div>
 
 <div id="dlg-buttons">
 	<a href="javascript:saveGoodsType()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
 	<a href="javascript:closeGoodsTypeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
 </div>
 
 <div id="dlg2" class="easyui-dialog" style="width: 500px;height:400px;padding: 10px 20px"
	  closed="true" buttons="#dlg-buttons2"  data-options="onClose:function(){resetValue()}">
	 	<form id="fm2" method="post">
			<input type="hidden"  id="token" name="token" value="" />
	 		<table cellspacing="8px">
	 			<tr>
	 				<td>所属类别：</td>
	 				<td>
	 					<input type="hidden" id="typeId" name="type.id"/>
	 					<input type="text" id="typeName"  class="easyui-validatebox" required="true" style="width: 90px;" readonly="readonly" onclick="chooseGoodsType()"/>
	 					<a href="javascript:chooseGoodsType()" class="easyui-linkbutton" iconCls="icon-search"  plain="true"></a>
	 				</td>
	 				<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
	 				<td>商品编码：</td>
	 				<td ><input type="text" id="code" name="code" class="easyui-validatebox" required="true" readonly="readonly" style="width: 120px;"/></td>
	 			</tr>
	 			<tr>
	 				<td>商品名称：</td>
	 				<td colspan="4">
	 					<input type="text" id="name" name="name" class="easyui-validatebox" required="true" style="width: 350px;"/>
	 				</td>
	 			</tr>
	 			<tr>
	 				<td>商品型号：</td>
	 				<td><input type="text" id="model" name="model" class="easyui-validatebox" required="true" style="width: 120px;"/></td>
	 				<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
	 				<td>单位：</td>
	 				<td>
	 					<select class="easyui-combobox" required="true" id="unit" name="unit"  style="width: 90px;" data-options="panelHeight:'auto',editable:false,valueField:'name',textField:'name',url:'/admin/goodsUnit/comboList'">
	 					</select>
	 					<a href="javascript:openChooseGoodsUnitDialog()" class="easyui-linkbutton" iconCls="icon-search"  plain="true"></a>
	 				</td>
	 			</tr>
	 			<tr>
	 				<td>采购价：</td>
	 				<td><input type="text" id="purchasingPrice" name="purchasingPrice" class="easyui-numberbox"  data-options="min:0" required="true" style="width: 120px;"/></td>
	 				<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
	 				<td>销售价：</td>
	 				<td><input type="text" id="sellingPrice" name="sellingPrice" class="easyui-numberbox" data-options="min:0" required="true" style="width: 120px;"/></td>
	 			</tr>
	 			<tr>
	 				<td>库存下限：</td>
	 				<td colspan="4">
	 					<input type="text" id="minNum" name="minNum" data-options="min:0" class="easyui-numberbox" required="true" style="width: 120px;"/>
	 				</td>
	 			</tr>
	 			<tr>
	 				<td>生产厂商：</td>
	 				<td colspan="4">
	 					<input type="text" id="producer" name="producer" class="easyui-validatebox" required="true" style="width: 350px;"/>
	 				</td>
	 			</tr>
	 			<tr>
	 				<td valign="top">备注：</td>
	 				<td colspan="4">
	 					<textarea rows="5" cols="47" id="remarks" name="remarks"></textarea>
	 				</td>
	 			</tr>
	 		</table>
	 	</form>
	</div>
	
	<div id="dlg-buttons2">
	    <a id="saveAndAddNextBuuton" href="javascript:saveGoods(2)" class="easyui-linkbutton" iconCls="icon-ok">保存并新增下一商品</a>
		<a href="javascript:saveGoods(1)" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
		<a href="javascript:closeGoodsDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>
 
 
   <div id="dlg3" class="easyui-dialog" style="width:180px;height:300px;padding: 10px 10px"
   closed="true" buttons="#dlg-buttons3">
   
   	<ul id="typeTree" class="easyui-tree"></ul>
   
  </div>
 
 <div id="dlg-buttons3">
 	<a href="javascript:saveGoodsTypeChoose()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">选择</a>
 	<a href="javascript:closeGoodsTypeChooseDialog()" class="easyui-linkbutton" iconCls="icon-cancel" plain="true">关闭</a>
 </div>
 
 	<div id="dlg4" class="easyui-dialog" style="width:200px;height:300px;"
  	 closed="true">
   
   		<table id="dg4"  class="easyui-datagrid" rownumbers="true" singleSelect="true"
		  url="/admin/goodsUnit/listAll" fitColumns="true" fit="true" toolbar="#tb2">
		   <thead>
		   	<tr>
		   		<th field="id" width="100" align="center" hidden=true>单位ID</th>
		   		<th field="name" width="100" align="center">单位名称</th>
		   	</tr>
		   </thead>
		 </table>
		 <div id="tb2">
		 	<div>
		 		<a href="javascript:openGoodsUnitAddDialog()" class="easyui-linkbutton"  plain="true">添加</a>
		 		<a href="javascript:deleteGoodsUnit()" class="easyui-linkbutton"  plain="true">删除</a>
		 		<a href="javascript:chooseGoodsUnit()" class="easyui-linkbutton"  plain="true">确定</a>
		 		<a href="javascript:closeGoodsUnitDialog()" class="easyui-linkbutton"  plain="true">取消</a>
		 	</div> 
		 </div>
   
   </div>
  
  <div id="dlg5" class="easyui-dialog" style="width:300px;height:120px;padding: 10px 20px"
   closed="true" buttons="#dlg-buttons5"  data-options="onClose:function(){$('#goodsUnitName').val('');}">
   
   <form id="fm5" method="post">
	<div>
		<input type="hidden"  id="token" name="token" value="" />
   		商品单位：&nbsp;<input type="text" id="goodsUnitName" name="name" class="easyui-validatebox" required="true"/>
	</div>
   	
   </form>
 </div>
 
 <div id="dlg-buttons5">
 	<a href="javascript:saveGoodsUnit()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
 	<a href="javascript:closeGoodsUnitAddDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
 </div>
 
</body>
</html>